
local pairs
    = pairs
    

module(...)


local x, y, nextX, nextY, prevX, prevY

function declarePosition(x, y)
  nextX, nextY = x, y
end

local currentState, previousState, nextState = {}, {}, {}

function declareButtonDown(btn)
  nextState[btn] = true
end

function declareButtonUp(btn)
  nextState[btn] = nil
end

function isButtonDown(btn)
  return not not currentState[btn]
end

function wasButtonStruck(btn)
  return currentState[btn] and not previousState[btn]
end

function wasButtonReleased(btn)
  return previousState[btn] and not currentState[btn]
end

function position()
  return x, y
end

function delta()
  if not (x and prevX) then
    return 0, 0
  else
    return x-prevX, y-prevY
  end
end

function refresh()
  x, y, prevX, prevY = nextX, nextY, x, y
  currentState, previousState, nextState = nextState, currentState, previousState
  -- nextState should be a copy of currentState
  for k,v in pairs(nextState) do
    nextState[k] = nil
  end
  for k,v in pairs(currentState) do
    nextState[k] = v
  end
end

local pointerVisible = true
local visiblePointerFunc

function visiblePointer(set)
  if (set ~= nil) then
    pointerVisible = set
    if visiblePointerFunc then
      visiblePointerFunc(set)
    end
  else -- get
    return pointerVisible
  end
end

function declareVisiblePointerFunc(func)
  visiblePointerFunc = func
end
